import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:intl/intl.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/utils/global_date_time_format.dart';
import 'package:flutter_syt/widgets/text_highliigh_widget.dart';

import '/theme/font_styles.dart';
import '/theme/theme_color.dart';

class FlowItemEntity {
  late String text;
  late int createTime;
  late String imageUrl;
  late String labelText;
  late String linkUrl;
  late int width;
  late int height;
  late bool showBig;
  late String author = '';
  late bool useEnDate = false;
  FlowItemEntity();

  factory FlowItemEntity.create(
    String text,
    int createTime,
    String imageUrl,
    String linkUrl, {
    String? labelText,
    int width = 0,
    int height = 0,
    bool showBig = false,
    String author = '',
  }) {
    return FlowItemEntity()
      ..text = text
      ..createTime = createTime
      ..imageUrl = imageUrl
      ..linkUrl = linkUrl
      ..labelText = labelText ?? ''
      ..width = width
      ..height = height
      ..showBig = showBig
      ..author = author;
  }
}

class FlowItem extends StatefulWidget {
  final FlowItemEntity entity;
  final String searchStr;
  const FlowItem(this.entity, {this.searchStr = '', Key? key})
      : super(key: key);

  @override
  _FlowItemState createState() => _FlowItemState();
}

class _FlowItemState extends State<FlowItem> {
  var zhLang = true;
  var local =
      appContext.storage.getString("storyLocal") ?? Intl.getCurrentLocale();
  @override
  Widget build(BuildContext context) {
    if (local.contains('zh')) {
      zhLang = true;
    } else {
      zhLang = false;
    }
    var date = zhLang == true
        ? formatDate(widget.entity.createTime)
        : enFormatDate(widget.entity.createTime);
    return Container(
        padding: EdgeInsets.fromLTRB(15, 15, 15, 12),
        decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(width: 0.5, color: ThemeColor.dividerColor)),
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
                child: Container(
                    constraints: BoxConstraints(minHeight: 60),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          alignment: Alignment.centerLeft,
                          child: TextHighlightWidget(
                              '${widget.entity.text}',
                              widget.searchStr,
                              FontStyles.headerTextStyle_16,
                              FontStyles.headerTextStyle_16
                                  .copyWith(color: ThemeColor.primaryColor)),
                          // child: Text('${widget.entity.text}',
                          //     style: FontStyles.headerTextStyle_16),
                        ),
                        SizedBox(
                          height: 12,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Expanded(
                              child: Text(
                                widget.entity.author,
                                // textAlign: TextAlign.end,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    fontSize: 11,
                                    color: ThemeColor.colorBlack40),
                              ),
                            ),
                            Container(
                              // constraints: BoxConstraints(maxWidth: 300.w),
                              child: Text(
                                date,
                                textAlign: TextAlign.end,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    fontSize: 11,
                                    color: ThemeColor.colorBlack40),
                              ),
                            )
                          ],
                        )
                        // padding: EdgeInsets.fromLTRB(0, 0, 14, 22),
                      ],
                    ))),
            SizedBox(
              width: 12,
            ),
            // Container(
            //   width: 80,
            //   height: 60,
            //   decoration: BoxDecoration(
            //     image: NetworkLoadImage.loadNetworkProviderImage(
            //         widget.entity.imageUrl),
            //     borderRadius: BorderRadius.circular(8),
            //   ),
            // )
            // NetworkLoadImage.loadNetworkProviderImage
            ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child:
                  //  CachedNetworkImage(
                  // "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4",
                  // imageUrl:
                  //   NetworkLoadImage.loadNetworkImage(
                  // widget.entity.imageUrl,
                  // width: 80,
                  // height: 60,
                  // fit: BoxFit.cover,
                  ExtendedImage.network(
                widget.entity.imageUrl,
                width: 80.w,
                height: 60.w,
                fit: BoxFit.cover,
                cacheHeight: 100,
                compressionRatio: 0.3,
                cache: false,
                loadStateChanged: ((ExtendedImageState state) {
                  switch (state.extendedImageLoadState) {
                    case LoadState.completed:
                      return state.completedWidget;
                    default:
                      return SizedBox();
                  }
                  // return SizedBox();
                }),
              ),
            ),
          ],
        )
        // child: Column(
        //   // crossAxisAlignment: CrossAxisAlignment.start,
        //   children: [
        //     // Expanded(
        //     // child:
        //     Row(
        //       crossAxisAlignment: CrossAxisAlignment.start,
        //       children: [
        //         Expanded(
        //             child: Container(
        //           // color: Colors.red,
        //           child: Text('${widget.entity.text}',
        //               style: FontStyles.headerTextStyle_16),
        //           padding: EdgeInsets.fromLTRB(0, 0, 14, 22),
        //         )),
        //         Container(
        //           child: Row(
        //             // mainAxisAlignment: MainAxisAlignment.start,
        //             children: [
        //               TextUtil.isEmpty(widget.entity.labelText)
        //                   ? Container()
        //                   : Container(
        //                       alignment: Alignment.center,
        //                       color: Color(0xFF3E89CE),
        //                       padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
        //                       margin: EdgeInsets.fromLTRB(5, 0, 5, 0),
        //                       child: Text(
        //                         '${widget.entity.labelText}',
        //                         textAlign: TextAlign.center,
        //                         style:
        //                             TextStyle(fontSize: 14, color: Colors.white),
        //                       ),
        //                     ),
        //               ClipRRect(
        //                 borderRadius: BorderRadius.circular(8),
        //                 child:
        //                     //  CachedNetworkImage(
        //                     // "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4",
        //                     // imageUrl:
        //                     NetworkLoadImage.loadNetworkImage(
        //                   widget.entity.imageUrl,
        //                   width: 80,
        //                   height: 60,
        //                   fit: BoxFit.cover,
        //                 ),
        //               ),
        //             ],
        //           ),
        //         ),
        //       ],
        //     ),
        //     // ),
        //     Row(
        //       mainAxisAlignment: MainAxisAlignment.end,
        //       children: [
        //         Container(
        //           constraints: BoxConstraints(maxWidth: 300.w),
        //           child: Text(
        //             widget.entity.author.isEmpty
        //                 ? date
        //                 : '${widget.entity.author}·$date',
        //             textAlign: TextAlign.end,
        //             overflow: TextOverflow.ellipsis,
        //             style:
        //                 TextStyle(fontSize: 11, color: ThemeColor.colorBlack40),
        //           ),
        //         )
        //       ],
        //     )
        //   ],
        // ),
        );
  }
}
